(function () {
    'use strict';

    /**
     * @ngdoc directive
     * @name webApp.directive:addStore
     * @description
     * # addStore
     */
    angular.module('webApp')
        .run(["$templateCache", "dataService",
            function ($templateCache, dataService) {
                var tplName = "views/directives.addstore.html";
                dataService.tpl(tplName).then(function (html) {
                    $templateCache.put(tplName, html);
                });
            }
        ])
        .directive('addStore', ["$rootScope", "helper", "$interval", function ($rootScope, helper, $interval) {
            return {
                restrict: 'EA',
                template: '<button ng-show="!storeList.length" ng-click="initData()" class="sui-btn btn-primary">{{btnName|translate}}</a>',
                scope: {
                    storeId: "@", //修改门店
                    callback: "&", //关闭回调
                    btnName: "@", //按钮名字
                },
                controller: ['$scope', 'authService', 'sessionService', 'dataService', '$compile', 'helper', '$templateCache', 'base64', '$timeout', 'ERP_CONFIG', function ($scope, authService, sessionService, dataService, $compile, helper, $templateCache, base64, $timeout, ERP_CONFIG) {
                    var intervaler;
                    $scope.btnName = $scope.btnName || "add_new";

                    function closeWin() {
                        if ($scope.addstoreWin) {
                            $scope.addstoreWin.modal("hide");
                        }
                        closeAll();
                    }

                    function closeAll() {
                        $rootScope.$broadcast("event:store-update");

                        if (intervaler) {
                            $interval.cancel(intervaler);
                        }
                        if ($scope.callback) {
                            $scope.callback();
                        }
                    }

                    $scope.initData = function () {
                        getImage().then(function () {
                            showLogin();
                        });
                    }

                    function getImage() {
                        var scence = "ESTO";
                        if ($scope.storeId) {
                            scence += $scope.storeId;
                        } else {
                            scence += "0";
                        }

                        return dataService.acodeunlimitGet(ERP_CONFIG.BackAppId, scence, true, "pages/admin/index/index").then(function (r) {
                            $scope.dataimage = "data:image/png;base64," + r;
                        });
                    }

                    function getstoreList() {
                        var query = {
                            page_no: 1,
                            page_size: 10
                        };
                        return dataService.storeGet(query).then(function (list) {
                            if (list && list.total_count) {
                                return true;
                            }
                        });
                    }


                    function showLogin() {
                        $scope.addstoreWin = $.alert({
                            backdrop: "static",
                            keyboard: false,
                            title: '<span>{{"使用微信创建或编辑门店"|translate}}</span>',
                            body: $templateCache.get("views/directives.addstore.html"),
                            okBtn: '{{"确定"|translate}}',
                            closeBtn: true,
                            hasfoot: false,
                            transition: true,
                            width: 'small',
                            show: function (e) {
                                $compile($(e.currentTarget).contents())($scope);

                                if (!$scope.storeId) {
                                    intervaler = $interval(function () {
                                        getstoreList().then(function (a) {
                                            if (a) {
                                                closeWin();
                                            }
                                        });
                                    }, 1000);
                                }
                            },
                            hide: function (e) {
                                closeAll();
                            },
                            shown: function (e) { }
                        });
                    }

                }],

                link: function (scope, elem, attrs, ctr) {

                }
            }
        }])

        .run(["$templateCache", "dataService",
            function ($templateCache, dataService) {
                var tplName = "views/directives.updatepickpoint.html";
                dataService.tpl(tplName).then(function (html) {
                    $templateCache.put(tplName, html);
                });
            }
        ])

        .directive('updatePickPoint', ["$rootScope", "helper", "$interval", function ($rootScope, helper, $interval) {
            return {
                restrict: 'EA',
                template: '<button ng-click="initData()" class="sui-btn btn-primary">{{btnName|translate}}</a>',
                scope: {
                    pickpointId: "@",
                    callback: "&", //关闭回调
                    btnName: "@", //按钮名字
                },
                controller: ['$scope', 'authService', 'sessionService', 'dataService', '$compile', 'helper', '$templateCache', 'base64', '$timeout', 'ERP_CONFIG', function ($scope, authService, sessionService, dataService, $compile, helper, $templateCache, base64, $timeout, ERP_CONFIG) {
                    var intervaler;
                    $scope.btnName = $scope.btnName || "add_new";

                    function closeWin() {
                        if ($scope.addstoreWin) {
                            $scope.addstoreWin.modal("hide");
                        }
                        closeAll();
                    }

                    function closeAll() {
                        if ($scope.callback) {
                            $scope.callback();
                        }
                    }

                    $scope.initData = function () {
                        getImage().then(function () {
                            showLogin();
                        });
                    }

                    function getImage() {
                        var scence = "PICK";
                        if ($scope.pickpointId) {
                            scence += $scope.pickpointId;
                        } else {
                            scence += "0";
                        }

                        if ($rootScope.store_id) {
                            scence += "ESTO" + $rootScope.store_id;
                        } else {
                            scence += "ESTO0";
                        }

                        //console.error("scene", scence);

                        return dataService.acodeunlimitGet(ERP_CONFIG.BackAppId, scence, true, "pages/admin/index/index").then(function (r) {
                            $scope.dataimage = "data:image/png;base64," + r;
                        });
                    }

                    function showLogin() {
                        $scope.addstoreWin = $.alert({
                            backdrop: "static",
                            keyboard: false,
                            title: '<span>{{"使用微信编辑发货点"|translate}}</span>',
                            body: $templateCache.get("views/directives.updatepickpoint.html"),
                            okBtn: '{{"确定"|translate}}',
                            closeBtn: true,
                            hasfoot: false,
                            transition: true,
                            width: 'small',
                            show: function (e) {
                                $compile($(e.currentTarget).contents())($scope);
                            },
                            hide: function (e) {
                                closeAll();
                            },
                            shown: function (e) { }
                        });
                    }
                }],

                link: function (scope, elem, attrs, ctr) {

                }
            }
        }]);
})();